package com.zhongjian.technologycal.mall.activity.web;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.bean.BaseBean;
import com.zhongjian.technologycal.mall.bean.CodeBean;
import com.zhongjian.technologycal.mall.bean.RegisterUserBean;
import com.zhongjian.technologycal.mall.http.BaseSubscriber;
import com.zhongjian.technologycal.mall.http.ExceptionHandle;
import com.zhongjian.technologycal.mall.http.MyApiService;
import com.zhongjian.technologycal.mall.http.RetrofitClient;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.Countdown;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;
import com.zhongjian.technologycal.mall.utils.ValidatorUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import rx.Subscriber;


/**
 * Created by wei on 2017/11/6.
 * 注册页面的实现
 */

public class ForgetPasswordActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;

    @BindView(R.id.reset_register_et_user_phone)
    EditText reset_register_et_user_phone;
    @BindView(R.id.new_reset_et_password)
    EditText new_reset_et_password;
    @BindView(R.id.again_new_reset_et_password)
    EditText again_new_reset_et_password;
    @BindView(R.id.reset_register_et_code)
    EditText reset_register_et_code;
    @BindView(R.id.reset_register_tv_commit_reg)
    TextView reset_register_tv_commit_reg;

    @BindView(R.id.reset_register_tv_send_code)
    TextView reset_register_tv_send_code;


    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("忘记密码");
    }

    @Override
    protected void initViewListener() {
        super.initViewListener();
    }

    @Override
    public void initPresenter() {

    }

    /**
     * 发送验证码
     */
    @OnClick(R.id.reset_register_tv_send_code)
    public void onClickRegisterSendCode() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else {

            if (TextUtils.isEmpty(reset_register_et_user_phone.getText().toString().trim())) {
                ToastUitl.showShort("手机号不能为空～");
                return;
            } else if (!ValidatorUtils.isMobile(reset_register_et_user_phone.getText().toString().trim())) {
                ToastUitl.showShort("手机号格式不正确～");
                return;
            } else {
                new Countdown(reset_register_tv_send_code, 60, "秒后重试", false);
                reset_register_tv_send_code.setClickable(false);
                MyApiService service = RetrofitClient.getInstance(ForgetPasswordActivity.this).create(MyApiService.class);
                Map<String, String> maps = new HashMap<String, String>();
                maps.put("phoneNum", reset_register_et_user_phone.getText().toString().trim());
                RequestBody body = RequestBody.create(
                        okhttp3.MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(maps));

                // execute and add observable to RxJava
                RetrofitClient.getInstance(ForgetPasswordActivity.this).createBaseApi().execute(
                        service.jsonSendCode(body), new BaseSubscriber<CodeBean>(ForgetPasswordActivity.this) {
                            @Override
                            public void onStart() {
                                super.onStart();
                                LoadingDialog.showDialogForLoading(ForgetPasswordActivity.this);
                            }

                            @Override
                            public void onError(ExceptionHandle.ResponeThrowable e) {
                                Log.e("Lyk", e.getMessage());

                            }

                            @Override
                            public void onNext(CodeBean codeBean) {
                                if (codeBean.getError_code() == 0) {
                                    ToastUitl.showShort(codeBean.getError_message());

                                } else {
                                    new Countdown(reset_register_tv_send_code, 0, "秒后重试", false);
                                    reset_register_tv_send_code.setClickable(true);
                                    ToastUitl.showShort(codeBean.getError_message());
                                }


                            }

                            @Override
                            public void onCompleted() {
                                super.onCompleted();
                                LoadingDialog.cancelDialogForLoading();
                            }
                        });
            }

        }


    }

    /**
     * 注册用户
     */
    @OnClick(R.id.reset_register_tv_commit_reg)
    public void onClickRegisterUser() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;

        } else {
            if (TextUtils.isEmpty(reset_register_et_user_phone.getText().toString().trim())) {
                ToastUitl.showShort("手机号不能为空～");
                return;
            } else if (!ValidatorUtils.isMobile(reset_register_et_user_phone.getText().toString().trim())) {
                ToastUitl.showShort("手机号格式不正确～");
                return;
            } else if (TextUtils.isEmpty(reset_register_et_code.getText().toString().trim())) {
                ToastUitl.showShort("验证码不能为空～");
                return;
            } else if (TextUtils.isEmpty(new_reset_et_password.getText().toString().trim())) {
                ToastUitl.showShort("密码不能为空～");
                return;
            } else if (TextUtils.isEmpty(again_new_reset_et_password.getText().toString().trim())) {
                ToastUitl.showShort("再次输入密码不能为空～");
                return;
            }else if(!new_reset_et_password.getText().toString().trim().equals(again_new_reset_et_password.getText().toString().trim())){
                ToastUitl.showShort("两次输入密码不一致～");
                return;
            }

            Map<String, String> maps = new HashMap<String, String>();
            maps.put("phoneNum", reset_register_et_user_phone.getText().toString().trim());
            maps.put("verifyCode", reset_register_et_code.getText().toString().trim());
            maps.put("password", new_reset_et_password.getText().toString().trim());
            maps.put("passwordAgain", again_new_reset_et_password.getText().toString().trim());
            RequestBody body = RequestBody.create(
                    okhttp3.MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(maps));

            HttpRetrofitClient.getInstance(getBaseContext()).postUrlRequestForgetPassword(body,
                    new Subscriber<ResponseBody>() {
                        @Override
                        public void onStart() {
                            super.onStart();
                        }

                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(ResponseBody responseBody) {
                            try {
                                Gson gson = new Gson();
                                BaseBean baseBean = gson.fromJson(responseBody.string(),BaseBean.class);
                                if(baseBean.getError_code() == 0){
                                    ToastUitl.showShort(baseBean.getError_message());
                                    finish();
                                }else{
                                    ToastUitl.showShort(baseBean.getError_message());
                                }

                            }catch (Exception e){
                                e.printStackTrace();
                            }

                        }
                    });


        }

    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

}
